//
//  LRJRecSecTraViewController.m
//  BreadTravel
//
//  Created by 李润嘉 on 15/12/22.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LRJRecSecTraViewController.h"
#import "LRJRecTravelDetailTableViewCell.h"
#import "LRJRecTravelDetailTextCell.h"
#import "LRJTravelDetailWaypointsModel.h"
#import "LRJTravelDetailDayModel.h"
#import "LRJTravelDetailModel.h"


@interface LRJRecSecTraViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UITableView *myTableView;

@property (nonatomic, strong)LRJTravelDetailModel *detailModel;

@property (nonatomic, assign)CGFloat allHeight;//tableview高度

@property (nonatomic, strong)UIImageView *bgImageView;//背景

@property (nonatomic, strong)UIImageView *headImageView;//用户头像

@property (nonatomic, strong)UILabel *name;//用户名

@property (nonatomic, strong)UILabel *writeName;//标题

@property (nonatomic, strong)UILabel *first_day;//起始日期

@property (nonatomic, strong)UILabel *day_count;//共多少天

@property (nonatomic, strong)UILabel *mileage;//里程

@property (nonatomic, strong)UILabel *recommendations;//喜欢

@property (nonatomic, strong)UIView *headView;//整个头部区域

@property (nonatomic, copy)NSString *myTitle;//区标题

@property (nonatomic, strong)UIImageView *backImage;//框


@end

@implementation LRJRecSecTraViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    UIImage *leftImage = [UIImage imageNamed:@"btn_nav_back@2x.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
    
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 480)];
    self.headView.backgroundColor = [UIColor colorWithRed:0.961 green:0.961 blue:0.902 alpha:1.000];
    
    
    [self createTableView];
    
    
    
}
//返回
-(void)leftButtonAction:(UIBarButtonItem *)bar{
    
    [self.navigationController popToRootViewControllerAnimated:NO];
    
}
#pragma mark -- 创建tableview
- (void)createTableView
{
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
        
    }];
    
    
    [self.myTableView registerClass:[LRJRecTravelDetailTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self.myTableView registerClass:[LRJRecTravelDetailTextCell class] forCellReuseIdentifier:@"textCell"];
    //请求数据
    [self getData];
}
#pragma mark -- 请求网络数据
- (void)getData
{
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hub show:YES];
    hub.labelText = @"加载中";
    
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/trips/%@", self.id];
    [AFNetworkTool getUrl:url body:nil result:HXJSON headerFile:nil success:^(id result)
    {
        
        self.detailModel = [LRJTravelDetailModel modelWithDictionary:result];
        
        //头文件布局
        [self setHeadView];
        
        [self.myTableView reloadData];
        
        [hub hide:YES];
        
    } failure:^(NSError *error) {
        
        NSLog(@"error : %@", error);
    }];
    
}
#pragma mark -- 头部View
-(void)setHeadView{
    
    self.myTableView.tableHeaderView = self.headView;

    
    self.bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.headView.W, 200)];
    [self.headView addSubview:self.bgImageView];
    
    //头部图片
    self.headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.bgImageView.W /2-30, self.bgImageView.H -25, 60, 60)];
    self.headImageView.layer.masksToBounds = YES;
    self.headImageView.layer.cornerRadius = 30;
    self.headImageView.layer.borderWidth = 2;
    self.headImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    [self.headView addSubview:self.headImageView];
    
    //作者名
    self.writeName = [[UILabel alloc]initWithFrame:CGRectMake(self.bgImageView.W/2-50, self.headImageView.Y+self.headImageView.H+15, 100, 16)];
    self.writeName.textAlignment = NSTextAlignmentCenter;
    self.writeName.alpha = 0.5;
    [self.headView addSubview:self.writeName];
    
    //标题
    self.name = [[UILabel alloc]initWithFrame:CGRectMake(30, self.writeName.H+self.writeName.Y+15, self.headView.W - 60, 60)];
    self.name.textAlignment = NSTextAlignmentCenter;
    self.name.numberOfLines = 0;
    [self.headView addSubview:self.name];
    
    self.backImage = [[UIImageView alloc] initWithFrame:CGRectMake(_headView.X + 10, self.name.Y+self.name.H+10, _bgImageView.W - 20, 100)];
    _backImage.image = [UIImage imageNamed:@"1111.png"];
    [self.headView addSubview:_backImage];
    
    
    
    //日期
    self.first_day = [[UILabel alloc]initWithFrame:CGRectMake(_backImage.X + 10, _backImage.Y + _backImage.H / 3, _backImage.W / 4 + 5, 16)];
    self.first_day.textAlignment = NSTextAlignmentCenter;
    self.first_day.font = [UIFont systemFontOfSize:15];
    [self.headView addSubview:self.first_day];
    
    //几天
    self.day_count = [[UILabel alloc]initWithFrame:CGRectMake(self.first_day.X, self.first_day.Y+self.first_day.H+5, self.first_day.W , 16)];
    self.day_count.textAlignment = NSTextAlignmentCenter;
    self.day_count.font = [UIFont systemFontOfSize:15];
    self.day_count.alpha = 0.5;
    [self.headView addSubview:self.day_count];
    
    //第二栏
    UILabel *labelMi = [[UILabel alloc]initWithFrame:CGRectMake(self.headView.W / 2 - _first_day.W / 2, _backImage.Y + _backImage.H / 3, _first_day.W, 16)];
    labelMi.text = @"里程";
    labelMi.textAlignment = NSTextAlignmentCenter;
    labelMi.font = [UIFont systemFontOfSize:15];
    [self.headView addSubview:labelMi];
    
    //里程数
    self.mileage = [[UILabel alloc]initWithFrame:CGRectMake(labelMi.X, labelMi.Y+labelMi.H+5, labelMi.W , 16)];
    self.mileage.textAlignment = NSTextAlignmentCenter;
    self.mileage.font = [UIFont systemFontOfSize:15];
    self.mileage.alpha = 0.5;
    [self.headView addSubview:self.mileage];
    
    //第三栏
    UILabel *labelLike = [[UILabel alloc]initWithFrame:CGRectMake(self.headView.W - labelMi.W - 20, _backImage.Y + _backImage.H / 3, _first_day.W, 16)];
    labelLike.text = @"喜欢";
    labelLike.textAlignment = NSTextAlignmentCenter;
    labelLike.font = [UIFont systemFontOfSize:15];
    [self.headView addSubview:labelLike];
    
    //喜欢个数
    self.recommendations = [[UILabel alloc]initWithFrame:CGRectMake(labelLike.X, labelLike.Y+labelLike.H+5, labelLike.W , 16)];
    self.recommendations.font = [UIFont systemFontOfSize:15];
    self.recommendations.textAlignment = NSTextAlignmentCenter;
    self.recommendations.alpha = 0.5;
    [self.headView addSubview:self.recommendations];
    
    //头部区域请求数据
    [self headViewGetData];
}
#pragma mark -- 头部区域请求数据
- (void)headViewGetData
{
    NSURL *url1 = [NSURL URLWithString:self.detailModel.cover_image];
    [self.bgImageView sd_setImageWithURL:url1 placeholderImage:IMAGE];
    
    NSURL *url2 = [NSURL URLWithString:self.detailModel.detailUserModel.avatar_m];
    [self.headImageView sd_setImageWithURL:url2 placeholderImage:IMAGE];
    
    self.name.text = self.detailModel.name;
    self.writeName.text = self.detailModel.detailUserModel.name;
    self.first_day.text = self.detailModel.first_day;
    self.day_count.text = [NSString stringWithFormat:@"%ld天",(long)self.detailModel.day_count];
    self.mileage.text = [NSString stringWithFormat:@"%.2ld",(long)self.detailModel.mileage];
    self.recommendations.text = [NSString stringWithFormat:@"%ld",(long)self.detailModel.recommendations];
}
#pragma mark -- tableView必须实现方法
//区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.detailModel.arrayDays.count;
}
//每个区里的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    LRJTravelDetailDayModel *dayModel = [self.detailModel.arrayDays objectAtIndex:section];

    return dayModel.arrayWay.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellIndentifier = @"cell";
    static NSString *cellIndentifierText = @"textCell";
    
    
    LRJTravelDetailWaypointsModel *wayModel = [[[self.detailModel.arrayDays objectAtIndex:indexPath.section] arrayWay] objectAtIndex:indexPath.row];
    
    if (wayModel.photoModel.h == 0) {
        LRJRecTravelDetailTextCell *textCell = [tableView dequeueReusableCellWithIdentifier:cellIndentifierText];
        textCell.wayModel = wayModel;
        //没有置灰效果
        textCell.selectionStyle = UITableViewCellAccessoryNone;
        return textCell;
    }
    else
    {
        LRJRecTravelDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
        cell.wayModel = wayModel;
        //没有置灰效果
        cell.selectionStyle = UITableViewCellAccessoryNone;
        return cell;

    }
    

    
}


#pragma mark -- cell行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LRJTravelDetailWaypointsModel *wayModel = [[[self.detailModel.arrayDays objectAtIndex:indexPath.section] arrayWay] objectAtIndex:indexPath.row];
    
    if (wayModel.photoModel.h == 0)
    {
        CGSize tempSize= [SizeforLabel labelRectWithSize:CGSizeMake(WIDTH - 20, MAXFLOAT) LabelText:wayModel.text Font:[UIFont systemFontOfSize:15]];
        CGFloat height = tempSize.height;
        return height + 100;
    }
    else
    {
        CGSize tempSize= [SizeforLabel labelRectWithSize:CGSizeMake(WIDTH - 20, MAXFLOAT) LabelText:wayModel.text Font:[UIFont systemFontOfSize:15]];
        CGFloat height = tempSize.height;
        
        CGSize tempImageSize = [LRJRecTravelDetailTableViewCell heightForImageModelWidth:wayModel.photoModel.w ModelHeight:wayModel.photoModel.h];
        CGFloat heightImage = tempImageSize.height;
        
        return height + heightImage + 50;
 
    }
}

#pragma mark -- 区标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    LRJTravelDetailDayModel *dayModel = [self.detailModel.arrayDays objectAtIndex:section];
    NSString *string = [dayModel.date substringFromIndex:5];
    NSString *stringTwo = [string stringByReplacingOccurrencesOfString:@"-" withString:@"月"];
    self.myTitle = [NSString stringWithFormat:@"- 第%@天 %@日 - ", dayModel.day, stringTwo];
    
    return _myTitle;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
